<?php
/**
 * The form used to manage the admin of each
 * groups.
 * @package fr.ec-lyon.aeecl.codebar.application.form
 * @subpackage admin
 * @author Théo Chamley
 */
class Application_Form_Admin_ModAdminGroupForm extends Zend_Form
{

	public function init()
	{
		$this->setAttrib('id', 'modadmingroup');

		$this->setMethod(Zend_Form::METHOD_POST)
		->setEnctype(Zend_Form::ENCTYPE_URLENCODED);

		$groups = Application_Model_Groupe::getDbTable()->fetchAll(
		Application_Model_Groupe::getDbTable()->select()
		->from(array('g' => Application_Model_Groupe::getDbTable()->getName()),
		array('id_groupe', 'nom'))
		->where('id_groupe <> ?', Application_Model_Groupe::ADMIN_GROUP_ID)
		->order('nom'));
			
		$select_group = new Zend_Form_Element_Select('group');
		$select_group->addMultiOption(-1, "Sélectionner un groupe");
		foreach($groups as $g) {
			$select_group->addMultiOption($g['id_groupe'], $g['nom']);
		}
		$select_group->setDecorators(array('ViewHelper','FormErrors','Errors'));

		$what = new Zend_Form_Element_Radio("what");
		$what->addMultiOptions(array(
		/*
		 * Do not touch the options or it will mess up the JS later.
		 */
		0 => 'Ajouter un administrateur',
		1 => 'Supprimer un administrateur'))
		->setDecorators(array('ViewHelper','FormErrors','Errors', 'Label'))
		->setAttrib("class", "radio-element");
			
		$autocomplete = new ZendX_JQuery_Form_Element_AutoComplete('user');
		$autocomplete->setFilters(array('StripTags'))
		->setJQueryParams(array(
		'autoFill' => true,
		'source' => 'searchusers',
		'minLength' => 3,
		'delay' => 100,
		'select' => new Zend_Json_Expr('function(event,ui) { displayInfo(ui.item.value) }'),
		'search' => new Zend_Json_Expr('function(event,ui) { removeInfo() }'),
		))
		->setDecorators(array('UiWidgetElement','FormErrors','Label','Errors'))
		->setAttrib("class", "menu-input");
		
		$pwd = new Zend_Form_Element_Password('pwd');
		$pwd->addFilter('StripTags')
		->addFilter('StringTrim')
		->setDecorators(array('ViewHelper','FormErrors','Label','Errors'))
		->setAttrib("class", "menu-input")
		->setAttrib("onKeyup", "pwdOk($('#pwd').val())");

		$pwd_verif = new Zend_Form_Element_Password('pwd_verif');
		$pwd_verif->addFilter('StripTags')
		->addFilter('StringTrim')
		->setDecorators(array('ViewHelper','FormErrors','Label','Errors'))
		->setAttrib("class", "menu-input")
		->setAttrib("onKeyup", "verifPwdOk($('#pwd_verif').val())");
		
		$select_admin = new Zend_Form_Element_Select('admin');
		$select_admin->addMultiOption(-1, "Sélectionner un admin")
			->setDecorators(array('ViewHelper','FormErrors','Errors'));
		
		$send = new Zend_Form_Element_Button('send');
		$send->setAttrib('id','send_button')
		->setLabel('Exécuter')
		->setAttrib('class','button')
		->setDecorators(array('ViewHelper','FormErrors','Errors'));

		$this->addElements(array($select_group, $what, $autocomplete, $pwd, $pwd_verif, $select_admin, $send));
	}
}